﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Item.Buff;
using Item;
using LitJson;

namespace ItemComponent
{
    public class BuffAdder : MyItemComponentMono
    {
        protected const string Key_BuffDatas = "BuffDatas";
        protected const string Key_BuffRange = "BuffRange";
        
        private ItemBase _item;
        private ST_ItemBuffData[] _arrBuffData;
        private int _range;
        [SerializeField,ReadOnly]
        private int _curRange;
        private string _guid;

        private void Start()
        {
            enabled = false;
        }

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            if (data == null || data[Key_BuffDatas] == null || data[Key_BuffRange] == null) return;

            InitBuffAdder(JsonMapper.ToObject<ST_ItemBuffData[]>(data[Key_BuffDatas].ToJson()), ((int)data[Key_BuffRange]));
        }

        protected BuffAdder InitBuffAdder(ST_ItemBuffData[] buffDatas, int range)
        {
            _range = range;
            _curRange = _range;

            if (_item == null)
                _item = GetComponent<ItemBase>();

            _guid = System.Guid.NewGuid().ToString();

            _arrBuffData = new ST_ItemBuffData[buffDatas.Length];
            for (int i = 0; i < buffDatas.Length; i++)
            {
                _arrBuffData[i] = buffDatas[i];
                _arrBuffData[i].strGUID = _guid;
            }

            return this;
        }

        private void OnEnable()
        {
            AddBuff();
        }
        private void OnDisable()
        {
            RemoveBuff();
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            //会栈溢出，来来回回的相互刷新buff

            //int targetRange = (int)(buffEffectData.fRange * _range);
            //if (targetRange != _curRange)
            //{
            //    RemoveBuff();
            //    _curRange = targetRange;
            //    AddBuff();
            //}
        }

        protected void AddBuff()
        {
            if (_item != null && _item.CurNode != null)
                Player.MyPlayer.Ins.MyFort.AddBuff(_item.CurNode.vIndex, _arrBuffData, _curRange);
        }
        protected void RemoveBuff()
        {
            if (_item != null && _item.CurNode != null)
                Player.MyPlayer.Ins.MyFort.RemoveBuff(_item.CurNode.vIndex, _guid, _curRange);
        }

        public override string GetDes()
        {
            SB.Clear();

            SB.Append("AddBuff:\n");
            for (int i = 0; i < _arrBuffData.Length; i++)
            {
                SB.Append(_arrBuffData[i].strBuffTag);
                SB.Append(' ');
                SB.Append(_arrBuffData[i].dBuffNum.ToString("0.0"));
                SB.Append('\n');
            }

            //去掉多余的 \n
            SB.Remove(SB.Length - 1, 1);

            return SB.ToString() ;
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_ItemBuffData[] buffDatas,int range)
        {
            JsonData json = new JsonData();
            json[Key_BuffDatas] = JsonMapper.ToObject(JsonMapper.ToJson(buffDatas));
            json[Key_BuffRange] = range;

            return new STMyComponetData()
            {
                strType = typeof(BuffAdder).FullName,
                data=json,
            };
        }
#endif
    }
}